package com.machine.filling.base.baseMvp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.machine.filling.MyApp;
import com.machine.filling.base.IView;
import com.machine.filling.function.main.activity.MainFuelActivity;


/**
 * 描述：基础继承Activity
 */
public abstract class BaseActivity<T extends IBasePresenter> extends AppCompatActivity implements IView, IBaseView<T> {
    protected Context mContext;
    protected Intent mIntent;
    protected Activity mActivity;

    protected T mPresenter;

    @SuppressWarnings("WrongConstant")
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        mContext = this;
        mActivity = this;
        mIntent = getIntent();
        initViews();
        initDatas();
        initScreenDisplayMeterics();
//        setRequestedOrientation(MyApp.getPreferenceProvider().getScreenOrientation());//强制竖屏
    }

    /**
     * 获取屏幕宽度、高度、密度  dp= px/density
     */
    private void initScreenDisplayMeterics(){
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int mScreenWidth = metric.widthPixels;
        int mScreenHeight = metric.heightPixels;
        float mDensity = metric.density;
        Log.d("屏幕信息：", "屏幕宽度：" + mScreenWidth + "\n屏幕高度：" + mScreenHeight + "\n屏幕密度：" + mDensity);
    }

    @Override
    public void showProgress(String loadingStr) {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void setPresenter(T presenter) {
        mPresenter = presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
        mPresenter.distoryContext();
    }

    //点击空白区域隐藏软键盘  @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                View view = getCurrentFocus();
                hideKeyboard(ev, view, BaseActivity.this);//调用方法判断是否需要隐藏键盘
                break;

            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public static void hideKeyboard(MotionEvent event, View view,
                                    Activity activity) {
        try {
            if (view != null && view instanceof EditText) {
                int[] location = { 0, 0 };
                view.getLocationInWindow(location);
                int left = location[0], top = location[1], right = left
                        + view.getWidth(), bootom = top + view.getHeight();
                // 判断焦点位置坐标是否在空间内，如果位置在控件外，则隐藏键盘
                if (event.getRawX() < left || event.getRawX() > right
                        || event.getY() < top || event.getRawY() > bootom) {
                    // 隐藏键盘
                    IBinder token = view.getWindowToken();
                    InputMethodManager inputMethodManager = (InputMethodManager) activity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(token,
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
